-- STARTING OVER FROM SCRATCH!!  WFWW 2.0! --

--credit line

--rActionButtonStyler texture lines (or see about how to register these buttons with rABS???)

local ObjectiveTrackerFrame = ObjectiveTrackerFrame
local minimizeButton = ObjectiveTrackerFrame.HeaderMenu.MinimizeButton
local otfHeader = ObjectiveTrackerFrame.HeaderMenu
local otfQuests = ObjectiveTrackerBlocksFrame.QuestHeader
local otfAchievements = ObjectiveTrackerBlocksFrame.AchievementHeader
local otfScenarios = ObjectiveTrackerBlocksFrame.ScenarioHeader
local otfBonus = BONUS_OBJECTIVE_TRACKER_MODULE.Header
local ScenarioStageBlock = ScenarioStageBlock
local LSM = LibStub("LibSharedMedia-3.0")
local widgetLists = AceGUIWidgetLSMlists
local GameTooltip = GameTooltip
local positions = {"TOP", "TOPRIGHT", "TOPLEFT",
					"BOTTOM", "BOTTOMRIGHT", "BOTTOMLEFT",
					"RIGHT", "LEFT", "CENTER"}
local fontFlags = {"None", "Outline", "Monochrome Outline"}	--, "Monochrome"}
local _,class = UnitClass("player")
local db, font, classColor
local WFWWSkinnedHeaders = {}
local WFWWSkinnedLines = {}
local tinsert = tinsert
local MoveObjectiveTracker, SetSize, Appearance, FontChanges

local OTFContainer = CreateFrame("ScrollFrame", "OTFContainer", UIParent, "UIPanelScrollFrameTemplate")
local OTFContainerBG = CreateFrame("Frame", nil, OTFContainer)
local OTFContainerScrollBar = OTFContainerScrollBar

local defaults = {
	bg = "Solid",
	border = "Blizzard Tooltip",
	borderWidth = 16,
	inset = 5,
	anchorPoint = "TOPRIGHT",
	xOffset = "-15",
	yOffset = "-250",
	height = 600,
	bgColor = {r = .1, g = .1, b = .1, a = .6},
	borderColor = {r = 1, g = 1, b = 1},
	borderAlpha = .8,
	hideArt = false,
	classBorder = false,
	font = "Friz Quadrata TT",
	fontSize = 12,
	fontFlag = "None",
	questSpacing = 10,
	lineSpacing = 4,
	disableShadow = false,
	scale = 1,
	hideFrame = false,
	instance = false,
	petBattle = false,
	scrolling = true,
	keybind = "",
}

local options = {
	name = "Who Framed Watcher Wabbit?",
	type = "group",
	args = {
		spacer = {
			name = " ",
			type = "description",
			width = "full",
			order = 0,
		},
		title1 = {
			name = "Size and Position",
			type = "header",
			order = .5,
		},
		anchor = {
			name = "Screen Anchor",
			desc = "What side of the screen you would like the WatchFrame located at. The tracker will also collapse in this direction.",
			type = "select",
			values = positions,
			get = function()
						for k, v in pairs(positions) do
							if db.anchorPoint == v then
								return k
							end
						end
					end,
			set = function(_, anchor)
						db.anchorPoint = positions[anchor]
						db.xOffset = "0"
						db.yOffset = "0"
						MoveObjectiveTracker()
					end,
			order = 1,
		},
		xOffset = {
			name = "X Offset",
			desc = "Offsets from the watch frame's anchor left/right",
			type = "input",
			get = function() return db.xOffset end,
			set = function(_,xOffset)
						if xOffset == nil or xOffset == "" then
							xOffset = "0"
						end
						db.xOffset = xOffset
						MoveObjectiveTracker()
					end,
			order = 2,
		},
		yOffset = {
			name = "Y Offset",
			desc = "Offsets from the watch frame's anchor up/down",
			type = "input",
			get = function() return db.yOffset end,
			set = function(_,yOffset)
						if yOffset == nil or yOffset == "" then
							yOffset = "0"
						end
						db.yOffset = yOffset
						MoveObjectiveTracker()
					end,
			order = 3,
		},
		scale = {
			name = "WatchFrame scale",
			desc = "The scale of the entire tracker frame.",
			type = "range",
			min = .5,
			max = 2,
			step = .05,
			get = function() return db.scale end,
			set = function(_, scale) 
						db.scale = scale
						SetSize()
					end,
			order = 4,
		},
		height = {
			name = "WatchFrame height",
			desc = "The maximum height of the tracker frame. A larger height can display more quests.",
			type = "range",
			min = 200,
			max = 1000,
			step = 5,
			get = function() return db.height end,
			set = function(_,height)
						db.height = height
						SetSize()
					end,
			order = 5,
		},
		spacer1 = {
			name = " ",
			type = "description",
			width = "full",
			order = 5.4,
		},
		title2 = {
			name = "Font Options",
			type = "header",
			order = 5.5,
		},
		font = {
			name = "Font",
			desc = "Select the font for the watch frame.",
			type = "select",
			dialogControl = "LSM30_Font",
			values = widgetLists.font,
			get = function()
					return db.font
				end,
			set = function(self, font)
					db.font = font
					FontChanges()
				end,
			order = 6,
		},
		fontSize = {
			name = "Text Size",
			desc = "Change the font size of the watch frame.",
			type = "range",
			min = 6,
			max = 24,
			step = 1,
			get = function() return db.fontSize end,
			set = function(_,size) 
						db.fontSize = size
						FontChanges()
					end,
			order = 7,
		},
		fontFlag = {
			name = "Font Flag",
			desc = "Set how to alter the watch frame's font.",
			type = "select",
			values = fontFlags,
			get = function()
						for k, v in pairs(fontFlags) do
							if db.fontFlag == v then
								return k
							end
						end
					end,
			set = function(_,key)
						db.fontFlag = fontFlags[key]
						FontChanges()
					end,
			order = 8,
		},
		disableShadow = {
			name = "Disable Font Shadow",
			desc = "If checked, the shadow effect on each line will be hidden.",
			type = "toggle",
			get = function() return db.disableShadow end,
			set = function()
						db.disableShadow = not db.disableShadow
						FontChanges()
					end,
			order = 8.5,
		},
--[[		questSpacing = {
			name = "Quest Spacing",
			desc = "Change the spacing between quests.",
			type = "range",
			min = 0,
			max = 12,
			step = 1,
			get = function() return db.questSpacing end,
			set = function(_,size)
						db.questSpacing = size
						DEFAULT_OBJECTIVE_TRACKER_MODULE.blockOffsetY = -db.questSpacing
						ObjectiveTracker_Update()
					end,
			order = 8.51,
		},]]
		--[[lineSpacing = {
			name = "Line Spacing",
			desc = "Change the spacing between lines.",
			type = "range",
			min = 0,
			max = 10,
			step = 1,
			get = function() return db.lineSpacing end,
			set = function(_,size)
						db.lineSpacing = size
						otfQuests.module.linespacing = db.lineSpacing
						otfAchievements.module.linespacing = db.lineSpacing
						--ObjectiveTracker_Update()
					end,
			order = 8.52,
		},]]
		spacer2 = {
			name = " ",
			type = "description",
			width = "full",
			order = 8.6,
		},
		title3 = {
			name = "Texture Options",
			type = "header",
			order = 8.7,
		},
		bg = {
			name = "Background",
			desc = "Texture to use as the tracker's background.",
			type = "select",
			dialogControl = "LSM30_Background",
			values = widgetLists.background,
			get = function()
					return db.bg
				end,
			set = function(self, bg)
					db.bg = bg
					Appearance()
				end,
			order = 9,
		},
		bgColor = {
			name = "Background Color/Alpha",
			desc = "The background color and transparency.\n\nIf you wish to use a custom background texture and do not want it colored, set this to white.",
			type = "color",
			hasAlpha = true,
			get = function() return db.bgColor.r, db.bgColor.g, db.bgColor.b, db.bgColor.a end,
			set = function(_,r,g,b,a)
					db.bgColor.r = r
					db.bgColor.g = g
					db.bgColor.b = b
					db.bgColor.a = a
					Appearance()
				end,
			order = 10,
		},
		hideArt = {
			name = "Hide Title Art",
			desc = "Hide the textures behind the section titles in the tracker.",
			type = "toggle",
			get = function() return db.hideArt end,
			set = function()
						db.hideArt = not db.hideArt
						Appearance()
					end,
			order = 10.5,
		},
		border = {
			name = "Border",
			desc = "Texture for the watch frame's border.",
			type = "select",
			dialogControl = "LSM30_Border",
			values = widgetLists.border,
			get = function()
					return db.border
				end,
			set = function(self, border)
					db.border = border
					Appearance()
				end,
			order = 11,
		},
		borderColor = {
			name = "Border Color",
			desc = "Color of the watch frame's border.",
			type = "color",
			disabled = function() return db.classBorder end,
			get = function() return db.borderColor.r, db.borderColor.g, db.borderColor.b end,
			set = function(_,r,g,b)
						db.borderColor.r = r
						db.borderColor.g = g
						db.borderColor.b = b
						Appearance()
					end,
			order = 12,
		},
		classBorder = {
			name = "Class Colored Border",
			desc = "Color the watch frame's border by your class.",
			type = "toggle",
			get = function() return db.classBorder end,
			set = function()
						db.classBorder = not db.classBorder
						Appearance()
					end,
			order = 12.5,
		},
		borderAlpha = {
			name = "Border Alpha",
			desc = "Transparency of the border.",
			type = "range",
			min = 0,
			max = 1,
			step = .05,
			get = function() return db.borderAlpha end,
			set = function(_, borderAlpha) 
						db.borderAlpha = borderAlpha
						Appearance()
					end,
			order = 13,
		},
		borderWidth = {
			name = "Border Width",
			desc = "Width of the border.",
			type = "range",
			min = 1,
			max = 24,
			step = .5,
			get = function() return db.borderWidth end,
			set = function(_, borderWidth) 
						db.borderWidth = borderWidth
						Appearance()
					end,
			order = 13.5,
		},
		inset = {
			name = "Inset",
			desc = "The inset of the background from the border texture.",
			type = "range",
			min = 0,
			max = 10,
			step = .5,
			get = function() return db.inset end,
			set = function(_, inset) 
						db.inset = inset
						Appearance()
					end,
			order = 14,
		},
		spacer3 = {
			name = " ",
			type = "description",
			width = "full",
			order = 14.4,
		},
		title4 = {
			name = "Miscellaneous",
			type = "header",
			order = 14.5,
		},
		hideFrame = {
			name = "Hide frame when empty",
			desc = "Hide the watch frame when no quests or achievements are being tracked.",
			type = "toggle",
			get = function() return db.hideFrame end,
			set = function()
						db.hideFrame = not db.hideFrame
						if db.hideFrame and GetNumTrackedAchievements() == 0 and GetNumQuestWatches() == 0 then
							OTFContainer:SetAlpha(0)
							minimizeButton:EnableMouse(false)
						else
							OTFContainer:SetAlpha(1)
							minimizeButton:EnableMouse(true)
						end
					end,
			order = 15,
		},
		instance = {
			name = "Collapse in instance",
			desc = "Automatically collapses the watch frame when entering an instance.",
			type = "toggle",
			get = function() return db.instance end,
			set = function()
						db.instance = not db.instance
						if db.instance then
							OTFContainer:RegisterEvent("PLAYER_ENTERING_WORLD")
						else
							OTFContainer:UnregisterEvent("PLAYER_ENTERING_WORLD")
						end
					end,
			order = 15.2,
		},
		petBattle = {
			name = "Collapse in pet battle",
			desc = "Collapses the watch frame when entering a pet battle instead it hiding completely.",
			type = "toggle",
			get = function() return db.petBattle end,
			set = function()
						db.petBattle = not db.petBattle
						if db.petBattle then
							OTFContainer:RegisterEvent("PET_BATTLE_OPENING_START")
							OTFContainer:RegisterEvent("PET_BATTLE_CLOSE")
							FRAMELOCK_STATES.PETBATTLES.ObjectiveTrackerFrame = "shown"
							FRAMELOCK_STATES.PETBATTLES.OTFContainer = "shown"
						else
							OTFContainer:UnregisterEvent("PET_BATTLE_OPENING_START")
							OTFContainer:UnregisterEvent("PET_BATTLE_CLOSE")
							FRAMELOCK_STATES.PETBATTLES.ObjectiveTrackerFrame = "hidden"
							FRAMELOCK_STATES.PETBATTLES.OTFContainer = "hidden"
						end
					end,
			order = 15.5,
		},
		scrolling = {
			name = "Scrolling Tracker",
			desc = "Display a longer list that you can scroll through with your mousewheel.",
			type = "toggle",
			get = function() return db.scrolling end,
			set = function()
						db.scrolling = not db.scrolling
						if db.scrolling then
							OTFContainerScrollBar:Show()
							OTFContainerScrollBar:SetAlpha(ObjectiveTrackerFrame.collapsed and 0 or 1)
							OTFContainer:EnableMouseWheel(true)
							OTFContainerBG:SetWidth(ObjectiveTrackerFrame.collapsed and 150 or 297)
							ObjectiveTrackerFrame:SetHeight(980 or db.height - 20)
						else
							OTFContainerBG:SetWidth(ObjectiveTrackerFrame.collapsed and 150 or 280)
							OTFContainerScrollBar:Hide()
							OTFContainerScrollBar:SetAlpha(0)
							OTFContainer:EnableMouseWheel(false)
							ObjectiveTrackerFrame:SetHeight(db.height - 20)
						end
					end,
			order = 15.6,
		},
		keybind = {
			name = "Keybinding",
			desc = "Key to minimize/maximize the tracker.",
			type = "keybinding",
			get = function() return db.keybind end,
			set = function(_,key)
						SetOverrideBinding(OTFContainer, false, db.keybind, nil)	--clear old binding
						if key ~= "" then	--set new
							SetOverrideBindingClick(OTFContainer, false, key, "WFWWkeybindButton")
						end
						db.keybind = key
					end,
			order = 15.7,
		},
		spacer4 = {
			name = " ",
			type = "description",
			width = "full",
			order = 16,
		},
		divider = {
			name = "Profile Management",
			type = "header",
			order = 17,
		},
		charSpec = {
			name = "Character specific settings",
			desc = "Have this character use their own profile.  If checked, any changes made will not affect other characters.\n\n|c00E30016WARNING:|r Your UI will be reloaded in the process!",
			type = "toggle",
			width = "full",
			confirm = true,
			get = function() return WFWWPCDB.charSpec end,
			set = function()
						WFWWPCDB.charSpec = not WFWWPCDB.charSpec
						ReloadUI()
					end,
			order = 18,
		},
		copyProfile = {
			name = "Copy from Default",
			desc = "Copy all settings from the default, global profile to this character's profile.  This will not effect other characters' specific profiles.\n\n|c00E30016WARNING:|r Your UI will be reloaded in the process!",
			type = "execute",
			confirm = true,
			disabled = function() return not WFWWPCDB.charSpec end,
			func = function()
						WFWWPCDB = WFWWDB
						WFWWPCDB.charSpec = true
						ReloadUI()
					end,
			order = 19,
		},
		resetProfile = {
			name = "Profile Reset",
			desc = "Reset this profile back to the out-of-the-box settings.  If you reset the character specific profile, the global profile will be untouched, and vice versa.  This will not effect other character specific profiles.\n\n|c00E30016WARNING:|r Your UI will be reloaded in the process!",
			type = "execute",
			confirm = true,
			func = function()
						if WFWWPCDB.charSpec then
							WFWWPCDB = {charSpec = true}
						else
							WFWWDB = {}
						end
						ReloadUI()
					end,
			order = 20,
		},
	},
}


function MoveObjectiveTracker()	--declared local at top of file
	OTFContainer:ClearAllPoints()
	OTFContainer:SetPoint(db.anchorPoint, UIParent, db.anchorPoint, db.xOffset, db.yOffset)
end

function SetSize()
	OTFContainer:SetHeight(db.height)
	OTFContainerBG:SetHeight(db.height + 13)
	OTFContainer:SetScale(db.scale)
	ObjectiveTrackerFrame:SetHeight(db.scrolling and 980 or db.height - 20)
end

local function ExpandObjectiveTracker()
	if ObjectiveTrackerFrame.collapsed then
		OTFContainer:SetHeight(db.height)
		OTFContainer:SetWidth(265)
		OTFContainerBG:ClearAllPoints()
		OTFContainerBG:SetPoint("TOPLEFT", -5, 5)
		if db.scrolling then
			OTFContainerScrollBar:Show()
			OTFContainerScrollBar:SetAlpha(1)
			OTFContainer:EnableMouseWheel(true)
			OTFContainerBG:SetSize(297, db.height + 13)
		else
			OTFContainerBG:SetSize(280, db.height + 13)
		end
		ObjectiveTracker_MinimizeButton_OnClick()	--default function called by OnClick
	end

	if GetNumTrackedAchievements() == 0 and GetNumQuestWatches() == 0 and GetNumAutoQuestPopUps() == 0 then
		--if our tracker is empty, still show "Objectives" text
		otfHeader.Title:Show()
	elseif not WFWWPCDB.collapsed then	--if not empty and not collapsed
		otfHeader.Title:Hide()
	end	
end

local function CollapseObjectiveTracker()
	if not ObjectiveTrackerFrame.collapsed then
		OTFContainer:SetHeight(30)
		OTFContainerBG:ClearAllPoints()
		OTFContainerBG:SetPoint("TOPRIGHT", 10, -2)
		OTFContainerBG:SetSize(150, 36)
		OTFContainerScrollBar:SetAlpha(0)
		OTFContainerScrollBar:Hide(0)
		OTFContainer:EnableMouseWheel(false)
		ObjectiveTracker_MinimizeButton_OnClick()	--default function called by OnClick
	end
end

function Appearance()
	OTFContainerBG:SetBackdrop({
			bgFile = LSM:Fetch("background", db.bg),
			edgeFile = LSM:Fetch("border", db.border),
			edgeSize = db.borderWidth,
			insets = {left=db.inset,right=db.inset,top=db.inset,bottom=db.inset}
		})
	OTFContainerBG:SetBackdropColor(db.bgColor.r,db.bgColor.g,db.bgColor.b,db.bgColor.a)
	if db.classBorder then
		OTFContainerBG:SetBackdropBorderColor(classColor.r,classColor.g,classColor.b,db.borderAlpha)
	else
		OTFContainerBG:SetBackdropBorderColor(db.borderColor.r,db.borderColor.g,db.borderColor.b,db.borderAlpha)
	end
	if db.hideArt then
		ObjectiveTrackerBlocksFrame.QuestHeader.Background:Hide()	
		ObjectiveTrackerBlocksFrame.AchievementHeader.Background:Hide()	
		ObjectiveTrackerBlocksFrame.ScenarioHeader.Background:Hide()	
		BONUS_OBJECTIVE_TRACKER_MODULE.Header.Background:Hide()	
	else
		ObjectiveTrackerBlocksFrame.QuestHeader.Background:Show()	
		ObjectiveTrackerBlocksFrame.AchievementHeader.Background:Show()	
		ObjectiveTrackerBlocksFrame.ScenarioHeader.Background:Show()	
		BONUS_OBJECTIVE_TRACKER_MODULE.Header.Background:Show()
	end
end

function FontChanges()
	font = LSM:Fetch("font", db.font)
	local shadow = db.disableShadow and 0 or 1
	
	otfHeader.Title:SetFont(font, db.fontSize, db.fontFlag)
	otfHeader.Title:SetShadowColor(0,0,0,shadow)
	otfQuests.Text:SetFont(font, db.fontSize+2, db.fontFlag)
	otfQuests.Text:SetShadowColor(0,0,0,shadow)
	otfAchievements.Text:SetFont(font, db.fontSize+2, db.fontFlag)
	otfAchievements.Text:SetShadowColor(0,0,0,shadow)
	otfScenarios.Text:SetFont(font, db.fontSize+2, db.fontFlag)
	otfScenarios.Text:SetShadowColor(0,0,0,shadow)
	otfBonus.Text:SetFont(font, db.fontSize+2, db.fontFlag)
	otfBonus.Text:SetShadowColor(0,0,0,shadow)
	ScenarioStageBlock.Stage:SetFont(font, db.fontSize+5, db.fontFlag)
	
	for k,v in pairs(WFWWSkinnedHeaders) do
		v:SetFont(font, db.fontSize, db.fontFlag)
		v:SetShadowColor(0,0,0,shadow)
	end
	for k,v in pairs(WFWWSkinnedLines) do
		v.Text:SetFont(font, db.fontSize, db.fontFlag)
		v.Text:SetShadowColor(0,0,0,shadow)
		if v.Dash then
			v.Dash:SetFont(font, db.fontSize, db.fontFlag)
			v.Dash:SetShadowColor(0,0,0,shadow)
		end
	end
	
--	ObjectiveTracker_Update()
end

local function SetUpContainer()
	OTFContainer:SetWidth(265)
	local OTFContainerScrollChild = CreateFrame("Frame", "OTFContainerScrollChild", OTFContainer)
	OTFContainerScrollChild:SetWidth(185)
	OTFContainerScrollChild:SetHeight(1000)
	OTFContainer:SetScrollChild(OTFContainerScrollChild)
	OTFContainerScrollBar.scrollStep = 35
	OTFContainerBG:SetPoint("TOPLEFT", -5, 5)
	OTFContainerBG:SetFrameStrata("BACKGROUND")

	ObjectiveTrackerFrame:SetParent(OTFContainerScrollChild)
	ObjectiveTrackerFrame:SetClampedToScreen(false)
	ObjectiveTrackerFrame:ClearAllPoints()
	ObjectiveTrackerFrame:SetPoint("TOPLEFT", 30, -10)
	local cap = ObjectiveTrackerFrame.ClearAllPoints
	local sp = ObjectiveTrackerFrame.SetPoint
	hooksecurefunc(ObjectiveTrackerFrame, "SetPoint", function(self)
		cap(self)
		sp(self, "TOPLEFT", 30, -10)
		self:SetHeight(db.scrolling and 980 or db.height - 20)
	end)
	
	if db.scrolling then
		OTFContainerBG:SetSize(297, db.height + 13)
	else
		OTFContainerBG:SetSize(280, db.height + 13)
		OTFContainerScrollBar:Hide()
		OTFContainerScrollBar:SetAlpha(0)
		OTFContainer:EnableMouseWheel(false)
	end

	--nUI code here if desired
	
	MoveObjectiveTracker()
	SetSize()
	Appearance()
	--SkinButtons()	--add later for rABS or Masque(?) support
	
	CreateFrame("Button", "WFWWkeybindButton")
	WFWWkeybindButton:SetScript("OnClick", function()
			if ObjectiveTrackerFrame.collapsed then
				ExpandObjectiveTracker()
			else
				CollapseObjectiveTracker()
			end
		end)
	if db.keybind ~= "" then
		SetOverrideBindingClick(OTFContainer, false, db.keybind, "WFWWkeybindButton")
	end
	
--	DEFAULT_OBJECTIVE_TRACKER_MODULE.blockOffsetY = -db.questSpacing
	--otfQuests.module.linespacing = db.lineSpacing
	--otfAchievements.module.linespacing = db.lineSpacing
		
	--make sure fonts for other modules are good, too
	
	FontChanges()
	local function HookForUpdates(module, block, objectiveKey, text, lineType, useFullHeight, hideDash, colorStyle)
		local header = block.HeaderText
		local line = block.lines[objectiveKey]
		--line.Text:SetWordWrap(false)
		--print(block.currentLine.Text:GetText())
		if header and not header.wfwwSkinned then
			header:SetFont(font, db.fontSize, db.fontFlag)
			header:SetShadowColor(0,0,0,db.disableShadow and 0 or 1)
			tinsert(WFWWSkinnedHeaders, header)
			header.wfwwSkinned = true
		end
		if line and not line.wfwwSkinned then
			line.Text:SetFont(font, db.fontSize, db.fontFlag)
			line.Text:SetShadowColor(0,0,0,db.disableShadow and 0 or 1)
			if line.Dash then
				line.Dash:SetFont(font, db.fontSize, db.fontFlag)
				line.Dash:SetShadowColor(0,0,0,db.disableShadow and 0 or 1)
			end
			tinsert(WFWWSkinnedLines, line)
			line.wfwwSkinned = true
		end
	end
	for i = 1, #ObjectiveTrackerFrame.MODULES do
		hooksecurefunc(ObjectiveTrackerFrame.MODULES[i], "AddObjective", HookForUpdates)
	end
	hooksecurefunc(SCENARIO_TRACKER_MODULE, "AddObjective", HookForUpdates)
	
	otfQuests.animateReason = 0
	otfAchievements.animateReason = 0
	
	minimizeButton:Show()
	hooksecurefunc(minimizeButton, "Hide", minimizeButton.Show)
	otfHeader:Show()
	hooksecurefunc(otfHeader, "Hide", minimizeButton.Show)
	
	hooksecurefunc("ObjectiveTracker_MinimizeButton_OnClick", function()
		WFWWPCDB.collapsed = ObjectiveTrackerFrame.collapsed
	end)
	
	if WFWWPCDB.collapsed then	--recall if watchframe was collapsed on last logout
		CollapseObjectiveTracker()
	else
		ExpandObjectiveTracker()
	end
	
	minimizeButton:RegisterForClicks("AnyDown")
	minimizeButton:SetScript("OnClick", function(self,btn)
		if IsShiftKeyDown() then
			ToggleAchievementFrame()
		elseif IsAltKeyDown() then
			InterfaceOptionsFrame_OpenToCategory("Who Framed Watcher Wabbit?")
			InterfaceOptionsFrame_OpenToCategory("Who Framed Watcher Wabbit?")
		elseif btn == "RightButton" then
			ToggleFrame(WorldMapFrame)
		else
			if not WFWWPCDB.collapsed then	--if the container's not collapsed, collapse it
				CollapseObjectiveTracker()
			else
				ExpandObjectiveTracker()
			end
		end
	end)
		
	minimizeButton:SetScript("OnEnter", function(self)
			GameTooltip:SetOwner(self, "ANCHOR_TOP")
			GameTooltip:AddLine("Who Framed Watcher Wabbit?",1,1,1)
			if ObjectiveTrackerFrame.collapsed then
				GameTooltip:AddLine("Left-click to expand the objectives tracker.")
			else
				GameTooltip:AddLine("Left-click to minimize the objectives tracker.")
			end
			GameTooltip:AddLine("Right-click to toggle the quest log.")
			GameTooltip:AddLine("Shift-click to toggle the achievements window.")
			GameTooltip:AddLine("Alt-click to open the Who Framed Watcher Wabbit? options.")
			GameTooltip:Show()
		end)
	minimizeButton:SetScript("OnLeave", function() GameTooltip:Hide() end)
	
--	ObjectiveTracker_Update()
end

local function SetUpDB()
	if WFWWPCDB.charSpec then
		--set defaults if new charSpec DB
		for k,v in pairs(defaults) do
			if type(WFWWPCDB[k]) == "nil" then
				WFWWPCDB[k] = v
			end
		end
		db = WFWWPCDB
	else
		db = WFWWDB
	end
end

local function Initialize()
	WFWWDB = WFWWDB or {}
	WFWWPCDB = WFWWPCDB or {}
		if WFWWPCDB.charSpec == nil then
			WFWWPCDB.charSpec = false
		end
		if WFWWPCDB.collapsed == nil then
			WFWWPCDB.collapsed = false
		end
	for k,v in pairs(defaults) do
	    if type(WFWWDB[k]) == "nil" then
	        WFWWDB[k] = v
	    end
	end
	SetUpDB()
	
	LibStub("AceConfig-3.0"):RegisterOptionsTable("Who Framed Watcher Wabbit?", options)
	OTFContainer.optionsFrame = LibStub("AceConfigDialog-3.0"):AddToBlizOptions("Who Framed Watcher Wabbit?", "Who Framed Watcher Wabbit?")
	SlashCmdList["WFWW"] = function()
			InterfaceOptionsFrame_OpenToCategory("Who Framed Watcher Wabbit?")
			InterfaceOptionsFrame_OpenToCategory("Who Framed Watcher Wabbit?")
		end
	SLASH_WFWW1 = "/wfww"
	
	classColor = CUSTOM_CLASS_COLORS and CUSTOM_CLASS_COLORS[class] or RAID_CLASS_COLORS[class]
	if CUSTOM_CLASS_COLORS then
		CUSTOM_CLASS_COLORS:RegisterCallback(function()
				if db.classBorder then
					Appearance()
				end
			end)
	end
	
	if not db.instance then
		OTFContainer:UnregisterEvent("PLAYER_ENTERING_WORLD")
	end
	if db.petBattle then
		OTFContainer:RegisterEvent("PET_BATTLE_OPENING_START")
		OTFContainer:RegisterEvent("PET_BATTLE_CLOSE")
		FRAMELOCK_STATES.PETBATTLES.ObjectiveTrackerFrame = "shown"
		FRAMELOCK_STATES.PETBATTLES.OTFContainer = "shown"
	else
		FRAMELOCK_STATES.PETBATTLES.OTFContainer = "hidden"
	end
	OTFContainer:RegisterEvent("QUEST_AUTOCOMPLETE")
	OTFContainer:RegisterEvent("QUEST_WATCH_LIST_CHANGED")
	OTFContainer:RegisterEvent("TRACKED_ACHIEVEMENT_LIST_CHANGED")
	local notCollapsed
	OTFContainer:SetScript("OnEvent", function(_, event, ...)
			if event == "PLAYER_ENTERING_WORLD" then
				if IsInInstance() and not WFWWPCDB.collapsed then
					--if you go in the instance and the WF isn't collapsed...
					CollapseObjectiveTracker()	--collapse
				end
			elseif event == "PET_BATTLE_OPENING_START" and not WFWWPCDB.collapsed then
				notCollapsed = true	--if it was open before pet battle
				CollapseObjectiveTracker()
			elseif event == "PET_BATTLE_CLOSE" and notCollapsed then
				ExpandObjectiveTracker()	--open again after pet battle if it was open before
				notCollapsed = false
			elseif event == "QUEST_WATCH_LIST_CHANGED" or event == "TRACKED_ACHIEVEMENT_LIST_CHANGED" then
				if GetNumTrackedAchievements() == 0 and GetNumQuestWatches() == 0 and GetNumAutoQuestPopUps() == 0 then
					--if our tracker is empty
					otfHeader.Title:Show()
					if db.hideFrame then
						OTFContainer:SetAlpha(0)
						minimizeButton:EnableMouse(false)
					end
				elseif not WFWWPCDB.collapsed then	--if not empty and not collapsed
					otfHeader.Title:Hide()
					if db.hideFrame then
						OTFContainer:SetAlpha(1)
						minimizeButton:EnableMouse(true)
					end
				end
			else
				local index = ...
				if event == "QUEST_AUTOCOMPLETE" or index == 0 then
					if WFWWPCDB.collapsed then
						if IsInInstance() and db.instance then
							return
						else
							ExpandObjectiveTracker()
						end
					end
				end
			end
		end)
	
end

OTFContainer:RegisterEvent("PLAYER_ENTERING_WORLD")
OTFContainer:SetScript("OnEvent", function()
		Initialize()
		SetUpContainer()
	end)